package com.i2bt.app.friend.model;

import com.i2bt.common.AndroidRestContract;

import com.i2bt.app.BaseActivity;
import com.i2bt.app.BaseJsonModel;
import com.i2bt.app.friend.FriendMessageActivity;
import com.i2bt.bean.FriendBean;
import com.i2bt.util.FriendMessageUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class FriendMessageJsonHandle extends BaseJsonModel{

	public FriendMessageJsonHandle(BaseActivity activity) {
		super(activity);
	}

	@Override
	protected String logicToWrap() throws JSONException {
		
		switch (beanParam.getAct()) {
		
		// 获取好友关注信息
		case AndroidRestContract.GET_FRIEND_MESSAGE : {
			jsonStrParam = wrapGetFriendMessage();
			break;
		}
		
		// 接受、拒绝好友信息
		case AndroidRestContract.ADD_FRIEND : {
			jsonStrParam = wrapAddFriendMessage();
			break;
		}
		
		// 删除好友信息
		case AndroidRestContract.DELETE_FRIEND_MESSAGE : {
			try {
				jsonStrParam = wrapDeleteFriendMessage();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			break;
		}
		
		}
		
		return jsonStrParam;
	}
	
	/**
	 * 打包获取好友信息
	 * @return
	 * @throws JSONException
	 */
	private String wrapGetFriendMessage() throws JSONException {
		
		wrapCommonJson();
		
		return jsonParam.toString();
	}

	 /**
	  * 打包发送和拒绝好友的信息
	  * @return
	  * @throws JSONException
	  */
	private String wrapAddFriendMessage() throws JSONException {

		FriendBean friendBean = (FriendBean) beanParam;
		wrapCommonJson();
		jsonParam
			.put(AndroidRestContract.FRIEND_MESSAGE_FLAG, friendBean.getFlag())
			.put(AndroidRestContract.FRIEND_MESSAGE_DATE, friendBean.getDate())
			.put(AndroidRestContract.FRIEND_MESSAGE_CONTENT, friendBean.getContent())
			.put(AndroidRestContract.FRIEND_MESSAGE_SENDER, friendBean.getSender())
			.put(AndroidRestContract.FRIEND_MESSAGE_RECEIVER, friendBean.getReceiver())
			.put(AndroidRestContract.FRIEND_MESSAGE_LONGTIME_FLAG, friendBean.getLongTimeFlag());

		return jsonParam.toString();

	}
	
	/**
	 * 打包删除好友信息
	 * @return
	 * @throws JSONException
	 * @throws UnsupportedEncodingException 
	 */
	private String wrapDeleteFriendMessage() throws JSONException, UnsupportedEncodingException {
		
		FriendBean friendBean = (FriendBean) beanParam;
		wrapCommonJson();
		jsonParam
			.put(AndroidRestContract.FRIEND_MESSAGE_SENDER, 
					URLEncoder.encode(friendBean.getSender(), "utf-8"))
			.put(AndroidRestContract.FRIEND_MESSAGE_RECEIVER, 
					URLEncoder.encode(friendBean.getReceiver(), "utf-8"))
			.put(AndroidRestContract.FRIEND_MESSAGE_LONGTIME_FLAG, friendBean.getLongTimeFlag());
		
		return jsonParam.toString();
	}

	@Override
	protected void logicToUnwrap(boolean isSuc, String result)
			throws JSONException {
		if (isSuc) {
			
			jsonParam = new JSONObject(result);
			if (jsonParam.getBoolean(AndroidRestContract.SUCCEED)) {
				
				FriendMessageActivity friendMessageActivity = 
						(FriendMessageActivity) activity;
				switch (jsonParam.getInt(AndroidRestContract.ACTION)) {
				
				//获取好友信息
				case AndroidRestContract.GET_FRIEND_MESSAGE : {
					friendMessageActivity.getFriendMessageResponse(unwrapGetFriendMessage());
				}
				
				//接受和拒绝好友信息
				case AndroidRestContract.ADD_FRIEND : {
					// 判断发送好友信息时是接受还是拒绝, 并调用对应的方法
					switch (((FriendBean) beanParam).getFlag()) {
					
					// 接受
					case FriendMessageUtil.ACCEPT : {
						friendMessageActivity.acceptFriendMessageResponse();
						break;
					}
					
					// 拒绝
					case FriendMessageUtil.REFUSE : {
						friendMessageActivity.refuseFriendMessageResponse();
						break;
					}
					
					}
					break;
				}
				
				//删除好友信息
				case AndroidRestContract.DELETE_FRIEND_MESSAGE : {
					friendMessageActivity.deleteFriendMessageResponse();
					break;
				}
				
				}
				
			}
			else {
				activity.toastHandler(jsonParam.getString(AndroidRestContract.MESSAGE));
			}
			
		}
		else {
			activity.toastHandler(result);
		}
	}
	
	/**
	 * 解包获取好友信息json
	 * @return
	 * @throws JSONException
	 */
	private List<FriendBean> unwrapGetFriendMessage() throws JSONException {
		
		List<FriendBean> friendMessageData = new ArrayList<FriendBean>();
		JSONArray jsonArrayTemp = jsonParam.getJSONArray(
				AndroidRestContract.FRIEND_MESSAGE);
		
		for (int i = 0; i < jsonArrayTemp.length(); i++) {
			friendMessageData.add(warpGetFriendMessageBean(
					jsonArrayTemp.getJSONObject(i)));
		}
		
		return friendMessageData;
		
	}
	
	/**
	 * 打包好友信息bean
	 * @param jsonObject
	 * @return
	 * @throws JSONException
	 */
	private FriendBean warpGetFriendMessageBean(JSONObject jsonObject) throws JSONException {
		FriendBean friendBean = new FriendBean();
		
		friendBean.setFlag(jsonObject.getInt(
				AndroidRestContract.FRIEND_MESSAGE_FLAG));
		friendBean.setDate(jsonObject.getString(
				AndroidRestContract.FRIEND_MESSAGE_DATE));
		friendBean.setSender(jsonObject.getString(
				AndroidRestContract.FRIEND_MESSAGE_SENDER));
		friendBean.setReceiver(jsonObject.getString(
				AndroidRestContract.FRIEND_MESSAGE_RECEIVER));
		friendBean.setContent(jsonObject.getString(
				AndroidRestContract.FRIEND_MESSAGE_CONTENT));
		friendBean.setLongTimeFlag(jsonObject.getString(
				AndroidRestContract.FRIEND_MESSAGE_LONGTIME_FLAG));
		FriendMessageUtil.messageContentDecoded(friendBean);
		
		return friendBean;
	}

}
